<template>
  <el-container class="admin-layout">
    <!-- 左侧菜单 -->
    <el-aside :width="isCollapse ? '64px' : '200px'" class="admin-aside">
      <div class="admin-logo">
        <img v-if="!isCollapse" src="@/assets/LoginImg/jng2.png" alt="Logo" />
        <span v-if="!isCollapse" class="admin-title">钓鱼助手管理系统</span>
      </div>
      <Sidebar :is-collapse="isCollapse" />
    </el-aside>

    <!-- 右侧主内容区 -->
    <el-container class="admin-main-container">
      <!-- 顶部导航栏 -->
      <el-header class="admin-header">
        <AdminHeader @toggle-collapse="toggleCollapse" />
      </el-header>

      <!-- 主内容区 -->
      <el-main class="admin-content">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import Sidebar from './Sidebar.vue'
import AdminHeader from './AdminHeader.vue'
import { useAuthStore } from '@/stores/authStore'
import request from '@/utils/request'

const isCollapse = ref(false)
const auth = useAuthStore()

const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

// 进入后台时，如未加载权限则拉取一次
onMounted(async () => {
  if (auth.isAdminLoggedIn && (!auth.adminInfo || !auth.adminPermissions?.length)) {
    try {
      const infoRes = await request.get('/admin/auth/info')
      if (infoRes.code === 0) {
        const admin = infoRes.data?.admin
        const permissions = infoRes.data?.permissions || []
        auth.setAdminInfo(admin)
        auth.setAdminPermissions(permissions)
      }
    } catch {}
  }
})
</script>

<style scoped>
.admin-layout {
  height: 100vh;
  width: 100vw;
}

.admin-aside {
  background: #001529;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.15);
  transition: width 0.3s ease;
}

.admin-logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 10px;
  background: #002140;
}

.admin-logo img {
  height: 40px;
  width: auto;
}

.admin-title {
  color: #fff;
  font-size: 18px;
  font-weight: bold;
  margin-left: 10px;
  white-space: nowrap;
}

.admin-main-container {
  display: flex;
  flex-direction: column;
}

.admin-header {
  background: #fff;
  border-bottom: 1px solid #f0f0f0;
  padding: 0 20px;
  display: flex;
  align-items: center;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.admin-content {
  background: #f0f2f5;
  padding: 20px;
  overflow-y: auto;
}
</style>

